Thread: Input error

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    4

    Input error

    This must be one of those things that people mess up on when they're tired o_o. Please help me find what's wrong with the following code:

    Code:
    #include <stdio.h>
    
    double pow(double base, int exp);
    int factorial(int num);
    
    main()
    {
        int n = 1;
        double term = 1, approximation = 1, exp;
    
        printf("Approximate e^x. x: ");
        scanf("%f", &exp);
        printf("EXP is %f\n", exp);
    
        getchar();
    }
    
    double pow(double base, int exp)
    {
    	int i;
    	double product = 1;
    	if(base == 0)
    		return 0;
    	for(i = 0; i < exp; i++)
    		product *= base;
    	return product;
    }
    
    int factorial(int num)
    {
    	int i, product = 1;
    	if(num == 0)
    		return 0;
    	for(i = 2; i <= num; i++)
    		product *= i;
    	return product;
    }
    Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    First of all, main() should return an int. Second of all, to get the user to input a double, you need to do:
    Code:
    scanf("%lf",%exp);

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    you're not calling pow nor factorial from anywhere....
    and
    Code:
     scanf("%lf", &exp);
    printf("EXP is %lf\n", exp);
    The same as bithub posted , but don't forget also the printf! float and double have different representations in memory.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    actually printf("%f",exp) is exactly the same as printf("%lf",exp).

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by bithub
    actually printf("%f",exp) is exactly the same as printf("%lf",exp).
    Except that behavior is undefined prior to C99.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I see nothing wrong with following the newest standard. Especially considering it's been out for over 5 years now.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I see nothing wrong with following the newest standard.
    It's best to limit yourself to a subset of features common to both standards until the new standard is widely implemented. This is especially important where a new standard feature is undefined behavior in the old standard.

    >Especially considering it's been out for over 5 years now.
    I use a state of the art compiler, but it doesn't compile C99. Just because the standard has been out for over five years doesn't mean that compilers will support it. In your work, feel free to use whatever you want; here, we assume C89 and stick with the common subset unless there's a strong reason not to.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I use a state of the art compiler, but it doesn't compile C99
    Just out of curiousity, what compiler do you use?

  9. #9
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    you prolly want another getchar() in there too, if you want to see what the result is. as it stands, scanf has input when you press enter. so its going to close most likely.
    Also on the nitpicky tip, Main has no arguments so it should be
    Code:
    int main(void)
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Just out of curiousity, what compiler do you use?
    Visual C++ .NET, primarily. Though I also make use of others, not nearly as much though.

    >Also on the nitpicky tip
    Getting really nitpicky, both versions are valid because main is a function definition, not a declaration. In a function definition, an empty parameter list does indeed mean "no arguments". As a stylistic issue, void should be used to maintain consistency with other functions that do have a declaration (in which case, void is required or the compiler will assume a K&R style function declaration, which is bad).

    Personally, I'd be more worried about the very real issue of undefined behavior from not returning a value rather than a style issue.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM